home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / ptf12.zip / PTFMAIN.SRC < prev    next >
Text File  |  1990-05-12  |  33KB  |  1,242 lines

  1. --::::::::::
  2. --ptf.a
  3. --::::::::::
  4. -- ..................................
  5. -- .                                .
  6. -- .  PTF (Portable_Text_Formatter) .  SPEC & BODY
  7. -- .                                .
  8. -- ..................................
  9. with Command_Line_Processor;
  10. with Command_Symbols;
  11. with Console;
  12. with Error_Log;
  13. with Index;
  14. with Output_File;
  15. with Variable;
  16. with Word_Processor;
  17. procedure Ptf is
  18.  
  19. --| Purpose
  20. --| Portable_Text_Formatter feeds the Word_Processor with files
  21. --| to process.
  22. --|
  23. --| Exceptions (none)
  24. --| Notes (none)
  25. --| Modifications
  26. --| 02/26/90  Rick Conn  Add Disable Underline, Disable All
  27. --| 02/26/90  Rick Conn  Fix check for number of file name arguments
  28. --|                      from 0-1 to 0-2 causing an error message
  29. --| 02/26/90  Rick Conn  Don't open output file if Cancel
  30. --| 05/07/90  Rick Conn  FILE_LISTER2 removed and CLP added
  31.  
  32.   Signon
  33.     : constant STRING
  34.       := "PTF, Version 1.2";
  35.  
  36.   Error_Log_File_Name
  37.     : constant STRING
  38.       := "ptf.err";
  39.  
  40.   File_Name_Length
  41.     : constant
  42.       := 100;
  43.  
  44.   subtype FILE_NAME_STRING is
  45.     STRING (1 .. File_Name_Length);
  46.  
  47.   File_Name
  48.     : FILE_NAME_STRING;
  49.  
  50.   File_Name_Last
  51.     : NATURAL;
  52.  
  53.   Valid_Options
  54.     : BOOLEAN
  55.       := true;
  56.  
  57.   Help_Displayed
  58.     : BOOLEAN
  59.       := false;
  60.  
  61.   Page_Offset
  62.     : NATURAL
  63.       := 0;
  64.  
  65.   Disable_Bolding_Flag
  66.     : BOOLEAN
  67.       := false;
  68.  
  69.   Disable_Underlining_Flag
  70.     : BOOLEAN
  71.       := false;
  72.  
  73.   Error_Logging_Flag
  74.     : BOOLEAN
  75.       := false;
  76.  
  77.   subtype OPTION_STRING is
  78.     STRING (1 .. 2);
  79.  
  80.   type OPTION_ID is
  81.     ( DISABLE_BOLDING, DISABLE_UNDERLINING, DISABLE_ALL,
  82.       DISPLAY_HELP, ERROR_LOGGING, SET_PAGE_OFFSET, UNKNOWN );
  83.  
  84.   type OPTION_TYPE is
  85.     record
  86.       Name           : OPTION_STRING;
  87.       Id             : OPTION_ID;
  88.     end record;
  89.  
  90.   type OPTION_LIST is
  91.     array (NATURAL range <>)
  92.       of OPTION_TYPE;
  93.  
  94.   Option
  95.     : constant OPTION_LIST
  96.       := (("da", DISABLE_ALL),
  97.           ("DA", DISABLE_ALL),
  98.           ("db", DISABLE_BOLDING),
  99.           ("DB", DISABLE_BOLDING),
  100.           ("du", DISABLE_UNDERLINING),
  101.           ("DU", DISABLE_UNDERLINING),
  102.           ("el", ERROR_LOGGING),
  103.           ("EL", ERROR_LOGGING),
  104.           ("h ", DISPLAY_HELP),
  105.           ("H ", DISPLAY_HELP),
  106.           ("PO", SET_PAGE_OFFSET),
  107.           ("po", SET_PAGE_OFFSET));
  108.  
  109.   use Command_Symbols;
  110.   use Word_Processor;                            -- visibility of "="
  111.  
  112.   package CLP renames Command_Line_Processor;
  113.  
  114.   -- ..................................
  115.   -- .                                .
  116.   -- .  Help_Message                  .  SPEC & BODY
  117.   -- .                                .
  118.   -- ..................................
  119.   procedure Help_Message is
  120.  
  121.   --| Purpose
  122.   --| Help_Message displays a help message to the user at the console.
  123.   --|
  124.   --| Exceptions (none)
  125.   --| Notes (none)
  126.  
  127.   begin -- Help_Message
  128.  
  129.     Console.Put_Line("Syntax: ptf [-option]* infile1 [infileN]* outfile");
  130.     Console.Put_Line("Options:");
  131.     Console.Put_Line("  da      Disable all backspacing features");
  132.     Console.Put_Line("  db      Disable boldface feature");
  133.     Console.Put_Line("  du      Disable underline feature");
  134.     Console.Put_Line("  el      Log errors to ptf.err");
  135.     Console.Put_Line("  poN     Offset pages by N columns");
  136.     Console.Put_Line("  h or H  This message");
  137.     Help_Displayed := true;
  138.  
  139.   end Help_Message;
  140.  
  141.   -- ..................................
  142.   -- .                                .
  143.   -- .  Is_File                       .  SPEC & BODY
  144.   -- .                                .
  145.   -- ..................................
  146.   function Is_File
  147.     ( Item           : in STRING )
  148.       return BOOLEAN is
  149.  
  150.   --| Purpose
  151.   --| Is_File returns TRUE if the Item does not begin with a dash.
  152.   --|
  153.   --| Exceptions (none)
  154.   --| Notes (none)
  155.  
  156.   begin -- Is_File
  157.  
  158.     return Item(Item'First) /= '-';
  159.  
  160.   end Is_File;
  161.  
  162.   -- ....................................
  163.   -- .                                  .
  164.   -- .  Process_Options                 .  SPEC & BODY
  165.   -- .                                  .
  166.   -- ....................................
  167.   procedure Process_Options
  168.     ( Item           : in STRING ) is
  169.  
  170.   --| Purpose
  171.   --| This routine processes the option if Item begins with a dash.
  172.   --|
  173.   --| Exceptions (none)
  174.   --| Notes
  175.   --|   Valid_Options may be modified if an option error occurred
  176.   --|   Disable_Bolding_Flag and Disable_Underlining_Flag may be modified
  177.   --|       if the DISABLE_ALL option is selected
  178.   --|   Disable_Bolding_Flag may be modified if that option is selected
  179.   --|   Disable_Underlining_Flag may be modified if that option is selected
  180.   --|   Error_Logging_Flag may be modified if that option is selected
  181.   --|   Page_Offset may be modified if that option is selected
  182.   --|
  183.   --|Options processed:
  184.   --|  -da        Disable all backspacing
  185.   --|  -db        Disable bolding (for drafts)
  186.   --|  -du        Disable underlining (for drafts)
  187.   --|  -el        Error logging to error.log
  188.   --|  -po#       Set initial page offset (default is 0)
  189.   --|  -H or -h   Display help message only
  190.  
  191.     Current_Option
  192.       : OPTION_ID
  193.         := UNKNOWN;
  194.  
  195.     Option_Text
  196.       : OPTION_STRING
  197.         := "  ";
  198.  
  199.   begin -- Process_Options
  200.  
  201.     if not Is_File(Item) then
  202.       if Item'Length >= 3 then
  203.         Option_Text := Item(Item'First + 1 .. Item'First + 2);
  204.       elsif Item'Length = 2 then
  205.         Option_Text(1) := Item(Item'First + 1);
  206.       end if;
  207.  
  208.       for I in Option'range loop
  209.         if Option_Text = Option(I).Name then
  210.           Current_Option := Option(I).Id;
  211.         end if;
  212.       end loop;
  213.  
  214.       case Current_Option is
  215.         when DISABLE_ALL =>
  216.           Disable_Bolding_Flag     := true;
  217.           Disable_Underlining_Flag := true;
  218.         when DISABLE_BOLDING =>
  219.           Disable_Bolding_Flag     := true;
  220.         when DISABLE_UNDERLINING =>
  221.           Disable_Underlining_Flag := true;
  222.         when DISPLAY_HELP =>
  223.           Help_Message;
  224.         when ERROR_LOGGING =>
  225.           Error_Logging_Flag       := true;
  226.         when SET_PAGE_OFFSET =>
  227.           begin
  228.             Page_Offset := NATURAL'Value(Item(Item'First+2 .. Item'Last));
  229.           exception
  230.             when others =>
  231.               Valid_Options  := false;
  232.               Page_Offset    := 0;
  233.           end;
  234.         when UNKNOWN =>
  235.           Valid_Options  := false;
  236.           Error_Log.Write_Error(Error_Invalid_Option);
  237.       end case;
  238.     end if;                                      -- not Is_File
  239.   end Process_Options;
  240.  
  241.   -- ....................................
  242.   -- .                                  .
  243.   -- .  Open_Output_File                .  SPEC & BODY
  244.   -- .                                  .
  245.   -- ....................................
  246.   function Open_Output_File
  247.       return BOOLEAN is
  248.  
  249.   --| Purpose
  250.   --| Open_Output_File makes sure the target file does not exist,
  251.   --| prompts the user if it does, and opens it for output.
  252.   --| This function returns TRUE if the output file is successfully
  253.   --| opened.
  254.   --|
  255.   --| Exceptions (none)
  256.   --| Notes (none)
  257.  
  258.     Cancel
  259.       : BOOLEAN
  260.         := false;
  261.  
  262.     Result
  263.       : Word_Processor.Operation_Status;
  264.  
  265.     Answer
  266.       : STRING (1 .. 10);
  267.  
  268.     Answer_Last
  269.       : NATURAL;
  270.  
  271.   begin -- Open_Output_File
  272.     if Output_File.Already_Exists(CLP.Output_File_Name) then
  273.       Console.Put_Line("  Output File " & CLP.Output_File_Name &
  274.                        " Exists");
  275.       Console.Put("  Erase it and proceed (Y/N)? ");
  276.       Console.Get_Line(Answer, Answer_Last);
  277.       if Answer_Last > 0 then
  278.         if (Answer(1) = 'y') or (Answer(1) = 'Y') then
  279.           if not Output_File.Delete(CLP.Output_File_Name) then
  280.             Error_Log.Write_Error(Error_Delete_File);
  281.             Cancel := true;
  282.           end if;
  283.         else
  284.           Error_Log.Write_Error(Error_User_Abort);
  285.           Cancel := true;
  286.         end if;
  287.       else
  288.         Error_Log.Write_Error(Error_User_Abor